/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package algorithm;

import java.util.ArrayList;

/**
 *
 * @author Nuf
 */
public class Population {

    private ArrayList<Solution> population;
    private Solution bestSolution;

    /**
     * This constructor creates a population with NumberOfSolutions solutions
     * using GlouGlouAlgo with random generation of the solutions
     *
     * @param numberOfSolutions
     * @param data
     */
    public Population(int numberOfSolutions, ProblemData data) {
        population = new ArrayList<>();
        for (int i = 0; i < numberOfSolutions; i++) {
            GlouGlouAlgo algo = new GlouGlouAlgo(data);
            Solution sol = new Solution(data.getPolyRessourceNb());
            algo.runRandom();
            sol.setSolution(algo.getSolution());
            population.add(sol);
        }
        findBestSolution();
    }

    public ArrayList<Solution> getPopulation() {
        return population;
    }

    public Solution getBestSolution() {
        return bestSolution;
    }

    public void setPopulation(ArrayList<Solution> pop) {
        this.population = pop;
        findBestSolution();
    }

    private void findBestSolution() {
        this.bestSolution = population.get(0);
        for (Solution sol : population) {
            if (sol.getSolutionEvaluation() < bestSolution.getSolutionEvaluation()) {
                this.bestSolution = sol;
            }
        }

    }

}
